import jwt
import datetime
from mddjango.settings import SECRET_KEY


def make_token(user_id):
    payload = {
        'user_id': user_id,
        'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1)
    }
    token = jwt.encode(payload=payload, key=SECRET_KEY)
    return token


def check_token(token):
    try:
        user_info = jwt.decode(token, SECRET_KEY)
    except jwt.exceptions.ExpiredSignatureError:
        return '签名已过期'
    except jwt.exceptions.DecodeError:
        return '无效令牌'
    return user_info
